package com.aiiju.core.service.tool;

import java.io.IOException;
import java.util.ArrayList;

import org.codehaus.jackson.JsonGenerationException;
import org.codehaus.jackson.map.JsonMappingException;
import org.codehaus.jackson.map.ObjectMapper;
import org.codehaus.jackson.map.annotate.JsonSerialize.Inclusion;
import org.junit.Test;

import com.bole.resumeparser.document.impl.DocumentResumeParserInterface;
import com.bole.resumeparser.exception.ResumeParseException;
import com.bole.resumeparser.html.impl.HtmlResumeParserInterface;
import com.bole.resumeparser.html.impl.LiePinResumeParser;
import com.bole.resumeparser.html.impl.ZhiLianResumeParser;
import com.bole.resumeparser.html.impl._51jobResumeParser;
import com.bole.resumeparser.models.ResumeData;
import com.bole.resumeparser.models.ResumeInformation;
import com.bole.resumeparser.models.TextResumeData;

public class ParseService {

	public String parseTxt(String context) throws Exception {
		
		ResumeInformation resumeInfo = new ResumeInformation();
		resumeInfo.setSource("others");
		resumeInfo.setType("txt");
		resumeInfo.setText(context);
		
		String person = this.getPerson(resumeInfo);
		System.err.println(person);
		return person;
	}
	
	public TextResumeData parseObj(String context) throws Exception {
		
		ResumeInformation resumeInfo = new ResumeInformation();
		resumeInfo.setSource("others");
		resumeInfo.setType("txt");
		resumeInfo.setText(context);
		
		 TextResumeData obj = this.getObj(resumeInfo);
		System.err.println(obj);
		return obj;
	}
	
	@Test
	public void zhiLianParse() throws Exception {
		ResumeInformation resumeInfo = new ResumeInformation();
		resumeInfo.setSource("zhilian");
		resumeInfo.setType("doc");
		
		// 文本内容需要进行研究
		resumeInfo.setText("");
		
		String person = this.getPerson(resumeInfo);
		System.err.println(person);
	}
	
	/**
	 * 这个是最早使用的方法
	 * @Description: 把文本直接转化成对象的结果
	 * @param resumeInfo 简历对象的
	 * @return
	 * @throws ResumeParseException
	 * @throws JsonGenerationException
	 * @throws JsonMappingException
	 * @throws IOException
	 * @author: aiju
	 * @time:2017年12月1日 上午11:58:51
	 */
	public String getPerson(ResumeInformation resumeInfo) throws ResumeParseException, JsonGenerationException, JsonMappingException, IOException {
		try{
			String text = resumeInfo.getText();
//			text = new String(text.getBytes("ISO-8859-1"),"utf-8");
			String source = resumeInfo.getSource();
			String type = resumeInfo.getType();
			
			HtmlResumeParserInterface parser = null;
			ResumeData re=new ResumeData();
			
			String jsonValue = "";
			
			if("html".equals(type)){
				switch(source){
				case "zhilian":
					parser = new ZhiLianResumeParser(text,"","");            		
					break;
				case "51job":
					parser=new _51jobResumeParser(text,"","");
					break;
				case "liepin":
					parser = new LiePinResumeParser(text,"",""); 
					break;
				}
				re=parser.parse();
				
				ObjectMapper mapper = new ObjectMapper();
				mapper.getSerializationConfig().setSerializationInclusion(Inclusion.NON_NULL);
				jsonValue = mapper.writeValueAsString(re);
//		    	jsonValue = jsonValue.replaceAll("\"_id\":\\{.*?\\}", "\"id\":\""+re.get_id().toString()+"\"");
			}else{
				DocumentResumeParserInterface docparser = null;
				TextResumeData textre = new TextResumeData();
				
				ArrayList<String> resumeContentList = new ArrayList<String>();
				String[] lines = text.split(System.getProperty("line.separator"));
				for(int i=0;i<lines.length;i++){
					String line = lines[i];
					line = line.replaceAll("\u00A0", " ").replaceAll("\u3000", " ").trim();
		        	line = line.replaceAll("：", ":");
					resumeContentList.add(line);
				}
				switch(source){
				case "zhilian":
					docparser = new com.bole.resumeparser.document.impl.ZhiLianResumeParser(resumeContentList);
					break;
				case "51job":
					docparser = new com.bole.resumeparser.document.impl._51jobResumeParser(resumeContentList);
					break;
				case "liepin":
					docparser = new com.bole.resumeparser.document.impl.LiepinResumeParser(resumeContentList);
					break;
				default:
					docparser = new com.bole.resumeparser.document.impl.TextResumeParser(resumeContentList);
					break;
				}
				textre=docparser.parse();
				
				ObjectMapper mapper = new ObjectMapper();
				mapper.getSerializationConfig().setSerializationInclusion(Inclusion.NON_NULL);
				jsonValue = mapper.writeValueAsString(textre);
			}
			
			String result = jsonValue;
			return result;
//			String response = new String(jsonValue.getBytes("utf-8"),"ISO-8859-1");
//			String result=response;
//			return result;
		}catch(Exception e){
			return "{\"status\":\"failed\"}";
		}
		
	}
	
	/**
	 * 把文本转化成对象
	 * @Description:
	 * @param resumeInfo
	 * @return
	 * @throws ResumeParseException
	 * @throws JsonGenerationException
	 * @throws JsonMappingException
	 * @throws IOException
	 * @author: aiju
	 * @time:2017年12月1日 上午11:58:32
	 */
	public TextResumeData getObj(ResumeInformation resumeInfo) throws ResumeParseException, JsonGenerationException, JsonMappingException, IOException {
		try{
			String text = resumeInfo.getText();
//			text = new String(text.getBytes("ISO-8859-1"),"utf-8");
			String source = resumeInfo.getSource();
			String type = resumeInfo.getType();
			
			HtmlResumeParserInterface parser = null;
			ResumeData re=new ResumeData();
			
			String jsonValue = "";
			
			if("html".equals(type)){
				switch(source){
				case "zhilian":
					parser = new ZhiLianResumeParser(text,"","");            		
					break;
				case "51job":
					parser=new _51jobResumeParser(text,"","");
					break;
				case "liepin":
					parser = new LiePinResumeParser(text,"",""); 
					break;
				}
				re=parser.parse();
				
				ObjectMapper mapper = new ObjectMapper();
				mapper.getSerializationConfig().setSerializationInclusion(Inclusion.NON_NULL);
				jsonValue = mapper.writeValueAsString(re);
//		    	jsonValue = jsonValue.replaceAll("\"_id\":\\{.*?\\}", "\"id\":\""+re.get_id().toString()+"\"");
			}else{
				DocumentResumeParserInterface docparser = null;
				TextResumeData textre = new TextResumeData();
				
				ArrayList<String> resumeContentList = new ArrayList<String>();
				String[] lines = text.split(System.getProperty("line.separator"));
				for(int i=0;i<lines.length;i++){
					String line = lines[i];
					line = line.replaceAll("\u00A0", " ").replaceAll("\u3000", " ").trim();
					line = line.replaceAll("：", ":");
					resumeContentList.add(line);
				}
				switch(source){
				case "zhilian":
					docparser = new com.bole.resumeparser.document.impl.ZhiLianResumeParser(resumeContentList);
					break;
				case "51job":
					docparser = new com.bole.resumeparser.document.impl._51jobResumeParser(resumeContentList);
					break;
				case "liepin":
					docparser = new com.bole.resumeparser.document.impl.LiepinResumeParser(resumeContentList);
					break;
				default:
					docparser = new com.bole.resumeparser.document.impl.TextResumeParser(resumeContentList);
					break;
				}
				textre=docparser.parse();
				
				return textre;
				
			/*	ObjectMapper mapper = new ObjectMapper();
				mapper.getSerializationConfig().setSerializationInclusion(Inclusion.NON_NULL);
				jsonValue = mapper.writeValueAsString(textre);*/
			}
			
		/*	String result = jsonValue;
			return result;*/
//			String response = new String(jsonValue.getBytes("utf-8"),"ISO-8859-1");
//			String result=response;
//			return result;
		}catch(Exception e){
//			return "{\"status\":\"failed\"}";
			return null;
		}
		return null;
		
	}
	
	/**
	 * 把对象转成json
	 * @Description:
	 * @param textre 需要进行转化的对象
	 * @return
	 * @throws JsonGenerationException
	 * @throws JsonMappingException
	 * @throws IOException
	 * @author: aiju
	 * @time:2017年12月1日 上午11:58:08
	 */
	public String objToJson(TextResumeData textre) throws JsonGenerationException, JsonMappingException, IOException {
		ObjectMapper mapper = new ObjectMapper();
		mapper.getSerializationConfig().setSerializationInclusion(Inclusion.NON_NULL);
		String jsonValue = mapper.writeValueAsString(textre);
		return jsonValue;
	}

}
